const request = require('request')
const fs = require('fs');

const fileUp = (params) => {
    return new Promise((resolve, reject) => {
        let url = 'https://pic.sogou.com/ris_upload'
        request.post({
            url,
            formData: params
        }, function (error, response, body) {
            if (error) {
                reject(error)
            }
            resolve(response)
        })
    })
}

const downImg = (params) => {
    return new Promise((resolve, reject) => {
        let name = '/tmp/' + new Date().getTime() + '.jpeg'
        request(params).pipe(fs.createWriteStream(name)).on('close', function () {
            resolve(name)
        })
    })
}

module.exports = {
    fileUp,
    downImg
}
